package com.sum.common.utils;

public class HexUtils {
    public HexUtils() {
    }

    public static boolean isHexDigit(char c) {
        return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
    }

    public static boolean isHexString(String str) {
        for(int i = 0; i < str.length(); ++i) {
            char c = str.charAt(i);
            if (!isHexDigit(c)) {
                return false;
            }
        }

        return true;
    }

    public static byte charToByte(char c) {
        c = Character.toUpperCase(c);
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static byte hex2Byte(String hex) {
        if (hex.length() < 2) {
            hex = "00" + hex;
        }

        if (hex.length() > 2) {
            hex = hex.substring(0, 2);
        }

        return (byte)(charToByte(hex.charAt(0)) << 4 | charToByte(hex.charAt(1)));
    }

    public static short hex2Short(String hex) {
        if (hex.length() < 4) {
            hex = "0000" + hex;
        }

        if (hex.length() > 4) {
            hex = hex.substring(0, 4);
        }

        return (short)(charToByte(hex.charAt(0)) << 12 | charToByte(hex.charAt(1)) << 8 | charToByte(hex.charAt(2)) << 4 | charToByte(hex.charAt(3)));
    }

    public static int hex2Int(String hex) {
        if (hex.length() < 8) {
            hex = "00000000" + hex;
        }

        if (hex.length() > 8) {
            hex = hex.substring(0, 8);
        }

        return charToByte(hex.charAt(0)) << 28 | charToByte(hex.charAt(1)) << 24 | charToByte(hex.charAt(2)) << 20 | charToByte(hex.charAt(3)) << 16 | charToByte(hex.charAt(4)) << 12 | charToByte(hex.charAt(5)) << 8 | charToByte(hex.charAt(6)) << 4 | charToByte(hex.charAt(7));
    }

    public static long hex2Long(String hex) {
        if (hex.length() < 16) {
            hex = "0000000000000000" + hex;
        }

        if (hex.length() > 16) {
            hex = hex.substring(0, 16);
        }

        long value = (long)(charToByte(hex.charAt(0)) << 28 | charToByte(hex.charAt(1)) << 24 | charToByte(hex.charAt(2)) << 20 | charToByte(hex.charAt(3)) << 16 | charToByte(hex.charAt(4)) << 12 | charToByte(hex.charAt(5)) << 8 | charToByte(hex.charAt(6)) << 4 | charToByte(hex.charAt(7)));
        value <<= 16;
        value |= (long)(charToByte(hex.charAt(8)) << 12 | charToByte(hex.charAt(9)) << 8 | charToByte(hex.charAt(10)) << 4 | charToByte(hex.charAt(11)));
        value <<= 16;
        value |= (long)(charToByte(hex.charAt(12)) << 12 | charToByte(hex.charAt(13)) << 8 | charToByte(hex.charAt(14)) << 4 | charToByte(hex.charAt(15)));
        return value;
    }

    public static String byte2Hex(byte b) {
        String rt = "";

        try {
            rt = Integer.toHexString(b & 255);
            if (rt.length() == 1) {
                rt = '0' + rt;
            }

            rt = rt.toUpperCase();
            return rt;
        } catch (Exception var3) {
            return "";
        }
    }

    public static String short2Hex(short value) {
        String rt = "";
        rt = byte2Hex((byte)(value >> 8 & 255));
        rt = rt + byte2Hex((byte)(value & 255));
        return rt;
    }

    public static String int2Hex(int value) {
        String rt = "";
        rt = short2Hex((short)(value >> 16 & '\uffff'));
        rt = rt + short2Hex((short)(value & '\uffff'));
        return rt;
    }

    public static String long2Hex(long value) {
        String rt = "";
        rt = int2Hex((int)(value >> 32));
        rt = rt + int2Hex((int)value);
        return rt;
    }

    public static byte[] hexString2Bytes(String hexString) {
        if (null != hexString && !hexString.isEmpty()) {
            String newString = "";

            int byteLength;
            for(byteLength = 0; byteLength < hexString.length(); ++byteLength) {
                char c = hexString.charAt(byteLength);
                if (isHexDigit(c)) {
                    newString = newString + c;
                }
            }

            if (newString.length() % 2 != 0) {
                newString = newString.substring(0, newString.length() - 1);
            }

            byteLength = newString.length() / 2;
            byte[] bytes = new byte[byteLength];
            int j = 0;

            for(int i = 0; i < bytes.length; ++i) {
                String hex = newString.substring(j, j + 2);
                bytes[i] = hex2Byte(hex);
                j += 2;
            }

            return bytes;
        } else {
            throw new IllegalArgumentException();
        }
    }

    public static String bytes2HexString(byte[] bArray, int offset, int length) {
        if (null != bArray && bArray.length > 0) {
            if (offset + length > bArray.length) {
                throw new IllegalArgumentException();
            } else if (offset >= 0 && length >= 0) {
                StringBuffer temp = new StringBuffer();

                for(int i = offset; i < offset + length; ++i) {
                    temp.append(byte2Hex(bArray[i]));
                }

                return temp.toString();
            } else {
                throw new IllegalArgumentException();
            }
        } else {
            throw new IllegalArgumentException();
        }
    }

    public static String bytes2HexString(byte[] bArray) {
        if (null != bArray && bArray.length > 0) {
            return bytes2HexString(bArray, 0, bArray.length);
        } else {
            throw new IllegalArgumentException();
        }
    }

    public static short bytes2Short(byte[] bArray, int offset) {
        if (null != bArray && bArray.length >= offset + 2) {
            return (short)(bArray[offset] << 8 & '\uff00' | bArray[offset + 1] & 255);
        } else {
            throw new IllegalArgumentException();
        }
    }

    public static short bytes2Short(byte[] bArray) {
        return bytes2Short(bArray, 0);
    }

    public static byte[] short2Bytes(short value) {
        byte[] result = new byte[]{(byte)(value >> 8 & 255), (byte)(value & 255)};
        return result;
    }

    public static int bytes2Int(byte[] bArray, int offset) {
        if (null != bArray && bArray.length >= offset + 4) {
            return bArray[offset] << 24 & -16777216 | bArray[offset + 1] << 16 & 16711680 | bArray[offset + 2] << 8 & '\uff00' | bArray[offset + 3] & 255;
        } else {
            throw new IllegalArgumentException();
        }
    }

    public static int bytes2Int(byte[] bArray) {
        return bytes2Int(bArray, 0);
    }

    public static byte[] int2Bytes(int value) {
        byte[] result = new byte[]{(byte)(value >> 24 & 255), (byte)(value >> 16 & 255), (byte)(value >> 8 & 255), (byte)(value & 255)};
        return result;
    }

    public static byte[] long2Bytes(long value) {
        byte[] result = new byte[]{(byte)((int)(value >> 56 & 255L)), (byte)((int)(value >> 48 & 255L)), (byte)((int)(value >> 40 & 255L)), (byte)((int)(value >> 32 & 255L)), (byte)((int)(value >> 24 & 255L)), (byte)((int)(value >> 16 & 255L)), (byte)((int)(value >> 8 & 255L)), (byte)((int)(value & 255L))};
        return result;
    }

    public static void main(String[] args) {
    }
}
